package cetc54.zhtx.server;

import cetc54.zhtx.start.ServerListenStatus;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;


/**
 * 服务器心跳处理
 * @author lipeng
 */
@Slf4j
public class ServerHeartBeatHandler extends SimpleChannelInboundHandler<String> {

    private ServerListenStatus serverListenStatus;

    public ServerHeartBeatHandler(ServerListenStatus serverListenStatus){
        this.serverListenStatus = serverListenStatus;
    }


    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        log.info("主机已连接，备机停止工作");
        //备机停止工作
        if(serverListenStatus !=null){
            //主机执行的工作
            serverListenStatus.masterWork();
        }

    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, String data) throws Exception {
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        log.info("备机开始工作");
        if(serverListenStatus !=null){
            //备机执行的工作
            serverListenStatus.standbyWork();
        }

    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        log.info("主机异常断开");
    }
}
